<?php

class Core_Backend_Component_EBook_Controller_Index extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $ebooks = App::getModel('media/collection_ebook')
            ->joinAccount()
            ->pagination($pageindex, $pagesize);
        if(!$this->getIdentity()->isAllowAnyAction(array('ebook_review', 'ebook_review'))){
            $videos->filterByCreatorId($this->getIdentity()->getId());
        }
        if ($validator->isNotEmpty('name')) {
            $videos->filterByName($validator->getValue('name'));
        }
        $ebooks->load();
        $records = $ebooks->loadCount();
        return $this->viewResult(array('list' => $ebooks, 'records' => $records));
    }

    public function createAction() {
        return $this->viewResult();
    }

    public function editAction($id) {
        $model = App::getModel('media/ebook')->load($id);
        return $this->createAction()->setModel($model);
    }

    public function savePostAction($status = 'draft') {
        $data = $this->getRequest()->getForm('ebook');
        $model = App::getModel('media/ebook');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        $savedModel = $model;
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('ebook/index/index'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return FALSE;
        }
    }

    public function reviewPostAction($ids, $status, $statusmessage = '') {
    	if($status == 'published'){
        	App::getModel('media/ebook')->publish($ids);
    	}elseif($status == 'forbidden'){
    		App::getModel('media/ebook')->forbid($ids, $statusmessage);
    	}
        return $this->redirectResult($this->getHistoryUrl('ebook/index/index'));
    }
}